
class Action {
  constructor (arr) {
    this.arr = arr
    this.morepositions = []
    this.same = false
  }
  insertNumber () {
    this.same = false
    let positions = []
    this.arr.forEach((row, rowIndex) => {
      row.forEach((colum, columIndex) => {
        if (colum === 0) {
          let position = {
            x: rowIndex,
            y: columIndex
          }
          positions.push(position)
        }
      })
    })
    if (positions.length != 0) {
      let index = Math.floor(Math.random() * positions.length)
      let dot = positions[index]
      this.arr[dot.x][dot.y] = (Math.round(Math.random()) + 1) * 2
    }
    this.judge()
    this.morepositions = positions
  }
  leftAction () {
    this.arr.forEach(row => {
      for(let left = 0; left < 3; left++){
        for(let right = 1; right < 4-left; right++){
          if(row[left] != 0 && row[left + right] != 0){
            if(row[left] == row[left + right]){
              row[left] = row[left] * 2
              row[left + right] = 0
            }
            break
          }
        }
      }
      for(let left = 0; left < 3; left++){
        if(row[left] === 0){
          for(let right = 1; right < 4 - left; right++){
            if(row[left + right] != 0){
              row[left] = row[left + right]
              row[left + right] = 0
              break
            }
          }
        }
      }
    })
  }
  rightAction () {
    this.arr.forEach(row => {
      for (let right = 3; right > 0; right--) {
        for (let left = 1; left <= right; left++) {
          if (row[right] != 0 && row[right - left] != 0) {
            if (row[right] == row[right - left]) {
              row[right] = row[right] * 2
              row[right - left] = 0
            }
            break
          }
        }
      }
      for (let right = 3; right > 0; right--) {
        if (row[right] === 0) {
          for (let left = 1; left <= right; left++) {
            if (row[right - left] != 0) {
              row[right] = row[right - left]
              row[right - left] = 0
              break
            }
          }
        }
      }
    })
  }
  upAction () {
    let arr = this.arr
    for(let colum = 0; colum < 4; colum++){
      for(let top = 0; top < 3; top++){
        for(let bottom = 1; bottom < 4 - top; bottom++){
          if(arr[top][colum] !=0 && arr[top + bottom][colum] != 0){
            if(arr[top][colum] == arr[top + bottom][colum]){
              arr[top][colum] = arr[top][colum] * 2
              arr[top + bottom][colum] = 0
            }
            break
          }
        }
      }
      for (let top = 0; top < 3; top++) {
        if (arr[top][colum] === 0) {
          for (let bottom = 1; bottom < 4 - top; bottom++) {
            if (arr[top + bottom][colum] != 0) {
              arr[top][colum] = arr[top + bottom][colum]
              arr[top + bottom][colum] = 0
              break
            }
          }
        }
      }
    }
  }
  downAction () {
    let arr = this.arr
    for (let colum = 0; colum < 4; colum++) {
      for (let bottom = 3; bottom > 0; bottom--) {
        for (let top = 1; top <= bottom; top++) {
          if (arr[bottom][colum] != 0 && arr[bottom - top][colum] != 0) {
            if (arr[bottom][colum] == arr[bottom - top][colum]) {
              arr[bottom][colum] = arr[bottom][colum] * 2
              arr[bottom - top][colum] = 0
            }
            break
          }
        }
      }
      for (let bottom = 3; bottom > 0; bottom--) {
        if(arr[bottom][colum] === 0) {
          for(let top = 1; top <= bottom; top++) {
            if (arr[bottom - top][colum] != 0) {
              arr[bottom][colum] = arr[bottom - top][colum]
              arr[bottom - top][colum] = 0
              break
            }
          }
        }
      }
    }
  }
  judge () {
    //判断是否有相邻相同
    let arr = this.arr
    arr.forEach(row => {
      for(let i = 0; i < 3; i++){
        if(row[i] === row[i+1]){
          this.same = true
        } 
      }
    })
    for(let colum = 0; colum < 4; colum++){
      for(let row = 0; row < 3; row++){
        if (arr[row][colum] === arr[row + 1][colum]){
          this.same = true
        }
      }
    }
  }
}
export default Action